package com.example.back.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtUtil {
    private static final String SECRET_KEY = "s9kXp2LmQ8z+C+F6hN+kP2sV5yD7aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789+="; // 替换成你的密钥
    private static final long EXPIRATION_TIME = 86400000; // 24小时

    public static String generateToken(String username) {
        return Jwts.builder()
                .setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .compact();
    }

    public static String validateToken(String token) {
        return Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(token)
                .getBody()
                .getSubject();
    }

    public static void main(String[] args) {
        String token = generateToken("admin");
        System.out.println(token);
        System.out.println(validateToken(token));
    }
}
